package main

import (
    "fmt"
    "time"
)

var c = make(chan int)
var count = 0
var retChannel = make(chan int)
var producerChan=make(chan int)

func main() {
	consumerNum:=1
	producerNum:=10

    for i := 0; i < consumerNum; i++ {
        go consumer(i)
    }
    for i := 0; i < producerNum; i++ {
         producer(c,i)
    }
    fmt.Println("c 开始关闭")
    close(c)

    time.Sleep(time.Second)

    for i := 0; i < consumerNum; i++ {
        fmt.Println("in for1 ")
        <-retChannel
         fmt.Println("in for2 ")
    }
    fmt.Println("after for")
    close(retChannel)
     fmt.Println("close retchannel")
    fmt.Println(count)

    time.Sleep(5*time.Second)
}
func consumer(index int) {
    for target := range c {
        fmt.Printf("no.%d:%d\n", index, target)
        time.Sleep(100*time.Millisecond)
        count++
    }
    fmt.Println(" c 关闭了")
    retChannel <- index
    fmt.Println(" consumer over")
}
func producer(c chan int,i int )  {
	 c <- i

}
